/**
 * 
 */
package de.fmannan.addbook.views;


import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import de.fmannan.addbook.domainmodel.GroupRegistrar;
import de.fmannan.addbook.domainmodel.IContact;

/**
 * The content provider class is responsible for providing objects to the view.
 * It can wrap existing objects in adapters or simply return objects as-is.
 * These objects may be sensitive to the current input of the view, or ignore it
 * and always show the same content (like Task List, for example).
 */
class TreeViewContentProvider implements IStructuredContentProvider,
		ITreeContentProvider {

	private static final Logger log = Logger.getLogger(TreeViewContentProvider.class);
	private final ContactTreeView contactTreeView;
	private GroupRegistrar invisibleRoot;
	
	TreeViewContentProvider(ContactTreeView contactTreeView) {
		this.contactTreeView = contactTreeView;
	}
	
	public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	}

	public void dispose() {
	}

	/**
	 * The parent is passed as an input to the View - but only once during
	 * initialization.
	 * 
	 * @param parent
	 *            an object domain model which is used for displaying
	 *            tree-objects
	 * @return an Array of Tree-Elements
	 */ 
	public Object[] getElements(Object parent) {
		// Provide own model which then can be used here - see table model
		// which is organized in a better way
		if (parent.equals(this.contactTreeView.getViewSite())) {
			// If there is not root yet, we initialize the tree model by using
			// the parent as our root
			if (invisibleRoot == null){

				try {
					/*
					 * Build up the data to display it in a tree. This should be
					 * done in a class which knows about group and contact objects,
					 * f.e. the GroupRegistrar.
					 */					
					GroupRegistrar.getInstance().startup();
					
				} catch (Exception e) {
					log.error("Error while parsing File", e);
				}
				invisibleRoot = GroupRegistrar.getInstance();

			}
			// ...and return the child-structure of the root element
			return getChildren(invisibleRoot);
		}
		// If the root already exists we again retrieve the child elements of
		// the parent object
		return getChildren(parent);
	}

	
	/**
	 * Returns the parent-element of a tree element. This method has to be
	 * implemented by each member of the object domain model such as contacts or
	 * groups.
	 */
	public Object getParent(Object child) {
		if (child instanceof IContact) {
			return ((IContact) child).getParent();
		}
		return null;
	}

	/**
	 * Retrieves an object array which contains all children of the given parent
	 * element. This method has to be implemented by each member of the object
	 * domain model such as contacts or groups.
	 */
	public Object[] getChildren(Object parent) {
		if (parent instanceof IContact) {
			return ((IContact) parent).getChildren();
		}
		return new Object[0];
	}

	/**
	 * This method has to be implemented by each member of the object domain
	 * model such as contacts or groups.
	 * 
	 * @return true if the given parent has children, false otherwise
	 */
	public boolean hasChildren(Object parent) {
		if (parent instanceof IContact) {
			return ((IContact) parent).hasChildren();			
		}
		return false;
	}
}